package com.tiyende.credit.loop.ui.adapter

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.utils.TiyendeImageUtil
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/21 12:02
 * @Annotation：绑卡适配器
 */
class TiyendeAuthenticationStepAccountAdapter(recyclerView: RecyclerView?): CommonRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        if (model.optBoolean("isEdit",false)){
            return R.layout.tiyende_authentication_step_account_edit_item
        }
        return R.layout.tiyende_authentication_step_account_index_item
    }

    override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.walletEdit) //点击编辑
        helper.setItemChildClickListener(R.id.walletDelete)//删除
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
        if (position == 0){
            val showText = "*${String.format(mContext.getString(R.string.tiyende_authentication_step_bind_account_index),position+1)}"
            val spString = SpannableString(showText)
            spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF4D3E")), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            helper.setText(R.id.walletIndex,spString)
        } else {
            helper.setText(R.id.walletIndex,String.format(mContext.getString(R.string.tiyende_authentication_step_bind_account_index),position+1))
        }

        //是否是编辑
        val isEdit = model.optBoolean("isEdit",true)
        if (!isEdit){
            helper.setText(R.id.walletTitle,model.optString("walletDesc"))
            helper.setText(R.id.walletMobile,"+" + model.optString("walletMobile"))

            val imgLogo = helper.getImageView(R.id.walletLogo)
            TiyendeImageUtil.instance?.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"), R.mipmap.tiyende_holder)

            //是否显示 删除按钮
            if (model.has("isShowDelete") && !model.optBoolean("isShowDelete")){
                helper.setVisibility(R.id.walletDelete, View.INVISIBLE)
            } else {
                helper.setVisibility(R.id.walletDelete, View.VISIBLE)
            }
        }
    }
}